package com.zj.leetcode;

import java.rmi.activation.ActivationGroup_Stub;

public class A67二进制求和 {
    public static void main(String[] args) {
        String a="11";
        String b="1";
        A67二进制求和 test1=new A67二进制求和();

        System.out.println(test1.addBinary(a,b));
    }
    public String addBinary(String a, String b) {
        boolean abig = a.length() > b.length();
        String shortstr = abig ? b : a;
        String longstr = abig ? a : b;

        char[] res = new char[longstr.length()];
        boolean add1 = false;
        for (int i = res.length - 1; i >= 0; i--) {
            int re = 0;
            if (longstr.charAt(i) == '1') re++;
            int index = i - (res.length - shortstr.length());
            if (index >= 0 && shortstr.charAt(index) == '1') re++;

            if (add1) re++;
            switch (re) {
                case 3:
                    res[i] = '1';
                    add1 = true;
                    break;
                case 2:
                    res[i] = '0';
                    add1 = true;
                    break;
                case 1:
                    res[i] = '1';
                    add1 = false;
                    break;
                case 0:
                    res[i] = '0';
                    add1 = false;
                    break;
            }

        }
        if(add1) return "1"+new String(res);
        return new String(res);
    }
}
